package com.lemei.cn.jsontookhttpandpost_get;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //这是get请求
        OkHttpClient okHttpClient_get = new OkHttpClient();
        // 02.请求体
        final Request request = new Request.Builder()
                .get()//get请求方式
                .url("http://10.0.3.2:8080/WebServiceTest/servlet/ServcieTest?name=sy")//网址
                .build();

        // 03.执行okhttp
        //同步请求往往不用
        // Response response = okHttpClient_get.newCall(request).execute();
        okHttpClient_get.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            //请求成功
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response != null) {
                    System.out.println(response.body().string());
                }
            }
        });


        //post请求
        OkHttpClient okHttpClient_post_kv = new OkHttpClient();

        // 定义请求体
        // 执行okhttp
        RequestBody body = new FormBody.Builder()
                .add("name", "sy")//添加参数体
                .add("age", "18")
                .build();
        Request build = new Request.Builder()
                .post(body) //请求参数
                .url("http://10.0.3.2:8080/WebServiceTest/servlet/ServcieTest")
                .build();
        okHttpClient_post_kv.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });


        //post请求json
        OkHttpClient okHttpClient_post_json = new OkHttpClient();
        String json = "{\n" + "    \"age\": \"18\",\n" + "    \"name\": \"sy\"\n" + "}";

        RequestBody body1 = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), json);
        Request request2 = new Request.Builder()
                .post(body)
                .url("http://10.0.3.2:8080/WebServiceTest/servlet/ServcieTest")
                .build();
        okHttpClient_post_json.newCall(request2).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });


        //上传图片
        OkHttpClient okHttpClient_upload = new OkHttpClient();

        File file = new File(Environment.getExternalStorageDirectory() + "/download", "file.txt");
        RequestBody body3 = RequestBody.create(MediaType.parse("application/octet-stream"), file);
        Request request3 = new Request.Builder()
                .post(body3)
                .url("http://10.0.3.2:8080/WebServiceTest/servlet/ServcieTest")
                .build();

        okHttpClient_upload.newCall(request3).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });


        //下载图片
        OkHttpClient okHttpClient_down = new OkHttpClient();
        Request request4 =
                new Request.Builder()
                        .get()
                        .url("http://10.0.3.2:8080/WebServiceTest/p22.jpg")
                        .build();
        okHttpClient_down.newCall(request4).enqueue(new Callback() {
            /**
             * 超时错误,服务器无响应
             *
             * @param call
             * @param e
             */
            @Override
            public void onFailure(Call call, IOException e) {

            }

            /**
             * 服务器响应
             *
             * @param call
             * @param response
             * @throws IOException
             */

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                InputStream inputStream = response.body().byteStream();
                final BitmapDrawable bitmapDrawable = new BitmapDrawable(inputStream);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        mIv_main_load_image.setImageDrawable(bitmapDrawable);

                    }
                });


            }
        }
    }
}